import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Button  } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Footer from '../index/footer'

import cat01 from '../../assets/kedouimg01/pcat02.png'
import cat02 from '../../assets/kedouimg01/nav07.png'
import cat03 from '../../assets/kedouimg01/sckf.png'
import pcat04 from '../../assets/kedouimg01/pcat04.png'

export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      goodslist:[],
      tjinfo:'',
      pageNumber:0,
      hasNextPage:false,
      scrollPd:false,
      pageheight:0,
	    znwns:'',
	    appmark:CommonJs.getmark(),
	    psx:'',
	    psxjg:false,
	    categoryId:'',
    }
  }
  config = {
    navigationBarTitleText:'加载中'
  }
  dobt(){
	if(this.state.appmark=='xjzb'){
		Taro.setNavigationBarTitle({title:'共享仓'})
	}
	else{
		Taro.setNavigationBarTitle({title:'云仓'})
	}
  }
  componentWillMount () {
	this.dobt()
  	var _this = this
    var byfrom=this.$router.params.byfrom;
  	if(byfrom){
  		this.setState({
	      byfrom:byfrom,
	      categoryId:this.$router.params.categoryId
	    },function(){
	    	_this.getgoodslist(0);
	    })
  	}
  	else{
  		this.getgoodslist(0);
  	}
    
    this.gettj()
    wx.showShareMenu({
	    withShareTicket: true,
	    menus: ['shareAppMessage']
	  });
  }
  componentDidShow () {
		
  }
  componentDidUpdate () {

  }
  gettj(){
  	var url='/yunwarehouse/papi/v1.9/ticket/getTicket/count'
  	var _this=this
		CommonJs.ajaxRequest(url,'','GET',function(res){
      _this.setState({
        tjinfo: res.data
      })
    },'wfw')
  }
	getgoodslist(pageNumber) {

	  if(this.state.byfrom=='pingtai'){
	  	if(this.state.categoryId){
	  		var url='/mallbean/papi/v1.9/item?categoryId='+this.state.categoryId+'&page='+pageNumber
	  	}
	  	else{
	  		var url='/mallbean/papi/v1.9/item?page='+pageNumber
	  	}
	  }
	  else{
    	var url='/mallbean/papi/v1.9/item?page='+pageNumber
    }
	  var sx=this.state.psx;
		if(sx){
			if(sx=='specialPriceSort'){
				url=url+"&"+sx+'='+this.state.psxjg;
			}
			else{
				url=url+"&"+sx+'=true';
			}
		}
    var _this = this
    var dates={
      size:10,
      page:pageNumber
    }
    CommonJs.ajaxRequest(url, dates, 'GET', function(res) {
      if (res) {

        if(res.data.totalPages>pageNumber){
          var hasNextPage = true;
        }
        else{
          var hasNextPage = false;
        }
        var result = res.data.list
        var results = _this.state.goodslist
        if(pageNumber == 0){
          results = []
        }
        for(var i = 0; i < result.length; i++){
          results.push(result[i]);
        }
        if (hasNextPage) {
          _this.setState({
            goodslist: results,
            znwns: '暂无数据',
            pageNumber: res.data.page,
            hasNextPage: hasNextPage,
            scrollPd: true
          })
        }
        else {
          _this.setState({
            goodslist: results,
            znwns: '暂无数据',
            pageNumber: res.data.page,
            hasNextPage: hasNextPage,
            scrollPd: false
          })
        }
      } else {
        _this.setState({
          znwns: '暂无数据'
        })
      }
    },'wfw')
  }
	loadMore(){
    var _this = this
    var pd = _this.state.scrollPd
    var isid = _this.state.isid
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1
      if(isid){
        _this.getGoodsByCatid(pageNumber,isid)
      }else{
        _this.getgoodslist(pageNumber)
      }
    }else{
      var hasNextPage =  _this.state.hasNextPage
      if(hasNextPage){
        Taro.showToast({
          title:'正在加载数据...',
          icon: 'none',
          duration: 2000
        })
      }else{ 
        Taro.showToast({
          title:'没有更多数据啦！',
          icon: 'none',
          duration: 2000
        })
      }
    }
  }
	goodsxq(index) {
  	var goodsid=this.state.goodslist[index].id
  	Taro.setStorage({key:'goodsid',data:goodsid+"|||UC"})
  	this.$preload({
	    malltype:'UC',
	    type:this.state.goodslist[index].id
	  })
    CommonJs.openTo('/pages/mall/goodsxq')
  }
	goopen(type,url){
	  this.$preload({
	    malltype:type
	  })
    CommonJs.openTo(url)
  }
	open(url){
    CommonJs.openTo(url);
  }
  opento(curl,id,sid,did){

    Taro.setStorage({key:'shopId',data:sid})
		Taro.setStorage({key:'dealerId',data:did})
		var url='/clusterShop/getClusterShopByShopId?shopId='+sid
  	var _this=this;
		CommonJs.ajaxRequest(url,'','GET',function(res){
			var dateinfo = Taro.getStorageSync('dateinfo')
			if(!dateinfo){
				dateinfo={}
			}
			var qid=''
			if(res.content){
				if(res.content.clusterId){
					qid=res.content.clusterId
				}
			}
			dateinfo.qid=qid
			Taro.setStorage({key:'dateinfo',data:dateinfo})
			if(_this.state.appmark=='xjzb'){
				_this.$preload({
		      type:id,
		      malltype:'UC'
		   }) 
			}
			else{
	  		_this.$preload({
		      type:id
		    })
	    }
	    CommonJs.openTo(curl);
		}) 
  }
	getnav(shopid,dealerId){
		var obj={
			type:1
		}
		this.$preload({
	      type:obj
	    })
    Taro.setStorage({key:'shopId',data:shopid})
		Taro.setStorage({key:'dealerId',data:dealerId})
		var url='/clusterShop/getClusterShopByShopId?shopId='+shopid
    var _this=this;
    CommonJs.ajaxRequest(url,'','GET',function(res){
    	//console.log(JSON.stringify(res))
    	var dateinfo=Taro.getStorageSync('dateinfo');
    	dateinfo.qid=res.content.clusterId
    	Taro.setStorage({key:'dateinfo',data:dateinfo})
	  	_this.open('/pages/mall/shopindex')
	  })
  }
	px(val){
  	if(val=='sx'){
  		Taro.showToast({
				title:'暂未开通此功能',
				icon: 'none',
				duration: 2000
		  });
  	}
  	else{
  		var lj=false;
  		if(val=='specialPriceSort'){
  			if(this.state.psxjg){
  				lj=false; 
  			}
  			else{
  				lj=true;
  			}
  		}
  		 var _this=this;
  	  this.setState({
				psx:val,
				psxjg:lj
		  },function(){
		  	_this.getgoodslist(0)
		  })
  	 
  	 
  	}
  }
  render () {
  	const goodslist  = this.state.goodslist 
	  const tjinfo = this.state.tjinfo
	  const appmark = this.state.appmark
    return (
    	<View className='wgmpage footerpage gxcpage'>
    		<ScrollView className='qun-goods' scrollY='true' style='height:100%' onScrollToLower={this.loadMore.bind(this,'')}>
    		<View className='gxq-box'>
    			<View className='gxc-tj'>
				<View className='gxc-tj-tit' style={appmark=='xjzb'?'':'display:none'}>共享仓统计(交易单数/交易商品量)</View>
				<View className='gxc-tj-tit' style={appmark=='xjzb'?'display:none':''}>云仓统计(交易单数/交易商品量)</View>
    				<View className='gxc-tj-li'>{tjinfo.count}<Text className="dw">单</Text></View>
    				<View className='gxc-tj-li gxc-tj-lits'></View>
    				<View className='gxc-tj-li'>{tjinfo.amount}<Text className="dw">件</Text></View>
    				<View className='gxc-tj-time'>数据统计截止到:{CommonJs.FormatDate(tjinfo.createAt,2)}</View>
    			</View>
    			<View className='gxc-btn'>
    				<View className='gxc-btn-li' onClick={this.open.bind(this,'/pages/cloud/cgorder')}> 
    					<Image className="img" mode="widthFix" src={cat01}></Image>
    					<Text className="btn-name">采购订单</Text>
    				</View>
    				<View className='gxc-btn-li' onClick={this.open.bind(this,'/pages/cloud/xsorder')}>
    					<Image className="img" mode="widthFix" src={cat02}></Image>
    					<Text className="btn-name">销售订单</Text>
    				</View>
    				<View className='gxc-btn-li' onClick={this.open.bind(this,'/pages/cloud/cgrecord')} style={appmark=='xjzb'?'':'display:none'}>
    					<Image className="img" mode="widthFix" src={pcat04}></Image>
    					<Text className="btn-name">采购记录</Text>
    				</View>
    				<View className='gxc-btn-li'  onClick={this.open.bind(this,'/pages/grouplist/grouplist?byfrom=gxc')}>
    					<Image className="img" mode="widthFix" src={cat03}></Image>
    					<Text className="btn-name">去采购</Text>
    				</View>
    			</View>
    			
    			<View className='goods-px' style='background-color:#fff;border-top:none;margin-top:20rpx;'>
						<View className={psx==''?'px-li pxlicur':'px-li'} onClick={this.px.bind(this,'')}>最新</View>
						<View className={psx=='saleAmountSort'?'px-li pxlicur':'px-li'} onClick={this.px.bind(this,'saleAmountSort')}>销量</View>
						<View className={psx=='specialPriceSort'?'px-li pxlicur':'px-li'} onClick={this.px.bind(this,'specialPriceSort')}>
								价格
								<View className='px-li-nr'>
								  <Text className={psxjg?'px-li-stb':'px-li-stb px-li-stbc'}></Text>
								  <Text className={psxjg?'px-li-xtb px-li-xtbc':'px-li-xtb'}></Text>
							  </View>
						</View>
						<View className='px-li' onClick={this.goopen.bind(this,'UC','/pages/mall/ucmall')}>分类</View>
				</View>
    			<View className='gxc-shop' style="background-color:#f1f1f1">
    					
	    				
							<View className='goodslist' style="margin-top:0;padding-bottom:170rpx;">
								<View className='nodate' style={!goodslist||goodslist.length==0?'':'display:none'}>该分类暂无商品</View>
								{goodslist.map((item, index) => (
								<View className='goods-li' onClick={this.goodsxq.bind(this,index)} style={item.saleStock.stockAmount>0?'':'display:none'}>
									<View className='goods-img'>
										<Image className='img' mode='widthFix' src={item.logoUrl?item.logoUrl:CommonJs.getlogo}></Image>
									</View>
									<View className='goods-text'>{item.name}</View>
									<View className='goods-pri'>
										<Text className='goods-bz'>UC</Text>
										<Text className='goods-jg'>{item.price.specialPrice/100}</Text>
										<Text className='goods-sy'>剩余：{item.saleStock.stockAmount}件</Text>
									</View>
								</View>
								))}
							</View>
    			</View>
    		</View>
				<Footer porpNav="2"></Footer>
				</ScrollView>
      </View>
    )
  }
}
